#!/usr/bin/python
# -*- coding: UTF-8 -*-
from functools import wraps

class logit(object):
    def __init__(self, logfile='out.log'):
        self.logfile = logfile

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            # func.__name__表示取函数的名字
            log_srting = func.__name__ + " was called"
            print(log_srting)
            # 打开logfile并写入
            try:
                with open(self.logfile, 'a') as opened_file:
                # 现在将日志打到指定的文件
                    opened_file.write(log_srting + '\n')
            except IOError as e:
                print(e)
            # 现在发送一个通知
            self.notify()
            return func(*args, **kwargs)
        return wrapped_function

    def notify(self):
        # logit只打日志，不做别的
        pass
class email_logit(logit):
    '''
    一个logit的实现版本，可以在函数调用时发送email给管理员
    '''
    def __init__(self, email='admin@myproject', *args, **kwargs):
        self.email = email
        super(email_logit, self).__init__(*args, **kwargs)

    def notify(self):
        # 发送一封email到self.email
        # 这里就不做实现了
        print('send')

@email_logit()
def myfunc1():
    print('func1')

@logit()
def myfunc2():
    print('func2')

# myfunc1()
# print('-' * 30)
myfunc2()